<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>点名器</title>
    <script src="js/xlsx.full.min.js"></script>
    <script src="js/jquery-3.5.1.js"></script>

    <script>
        $(function () {
            $("#btn").click(function () {
                $.post("getStudent",function (data) {
                    var success = data.isSuccess;
                    if (success === "0"){
                        //失败
                        var err = data.err;
                        alert(err);
                    }else {
                        var name = data.name;
                        $("#name").val(name);
                    }
                },"json");
            });
        })

    </script>
</head>
<body>
文件：<br>
<input type="file" id="file"><br>
<br>
<input type="button" value="点名" id="btn">
<input type="text" disabled="disabled" id="name">


</body>

<script>
    //添加一个监听事件,一旦修改,则执行以下函数
    document.querySelector("#file").addEventListener("change",function (){

        //files 返回一个文件数组对象,数组长度为1
        var f = this.files[0];
        var name = f.name.split(".");
        //判断上传的是否是excel文件
        var type = name[name.length-1];

        //判断上传的是否是excel文件
        if (type !== "xlsx" && type !== "xls"){
            alert("仅支持excel文件");
            return false;
        }
        var reader = new FileReader();

        //只会读取有内容的部分
        reader.readAsBinaryString(f);
        var data = "";
        reader.onload = function (e){
            var result = e.target.result;
            var read = XLSX.read(result,{type:"binary"}).Strings;
            for (var i = 0; i < read.length; i++) {
                data = read[i].h+"#"+data;
            }
            $.post("addStudent", {"name": data});
        }

    })


</script>

</html>